package com.bytedance.ttnet.hostmonitor;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.bytedance.ttnet.hostmonitor.Logger.LogLevel;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Iterator;

public class HostMonitor extends IntentService {
    public HostMonitor() {
        super("HostMonitor");
    }

    public static Intent a(Context arg2) {
        Intent v0 = new Intent(arg2, HostMonitor.class);
        v0.setAction("com.bytedance.ttnet.hostmonitor.check");
        return v0;
    }

    static void a(Context context, ConnectionType connectionType) {
        try {
            Intent intent = new Intent(context, HostMonitor.class);
            intent.setAction("com.bytedance.ttnet.hostmonitor.check");
            intent.putExtra("com.bytedance.ttnet.hostmonitor.connection_type", connectionType.ordinal());
            context.startService(intent);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    private static boolean a(b arg5, int arg6) {
        Socket v1_2 = null;
        try {
            v1_2 = new Socket();
            v1_2.connect(new InetSocketAddress(arg5.a, 80), arg6);
            return true;
        } catch (Exception v0_2) {


            return false;
        } finally {
            if (v1_2 != null) {
                try {
                    v1_2.close();
                } catch (Exception v1_1) {
                    Logger.a("HostMonitor", "Error while closing socket.");
                }
            }
        }
    }

    public static void b(Context arg1) {
        try {
            arg1.startService(HostMonitor.a(arg1));
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    static ConnectionType c(Context arg4) {
        ConnectionType v0_2;
        NetworkInfo v0 = ((ConnectivityManager) arg4.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (v0 == null || !v0.isConnected()) {
            v0_2 = ConnectionType.NONE;
        } else {
            int v0_1 = v0.getType();
            if (v0_1 == 0) {
                v0_2 = ConnectionType.MOBILE;
            } else if (v0_1 == 1) {
                v0_2 = ConnectionType.WIFI;
            } else {
                String v1 = "HostMonitor";
                String v0_3 = "Unsupported connection type: " + v0_1 + ". Returning NONE";
                if (com.bytedance.ttnet.hostmonitor.Logger.b.a().a.compareTo(LogLevel.ERROR) <= 0) {
                    com.bytedance.ttnet.hostmonitor.Logger.b.a();
                    Log.e(v1, v0_3);
                }

                v0_2 = ConnectionType.NONE;
            }
        }

        return v0_2;
    }

    private void a(ConnectionType arg10, e arg11) {
        Logger.a("HostMonitor", "Starting reachability check");
        Iterator v5 = arg11.b().keySet().iterator();
        while (v5.hasNext()) {
            Object v0 = v5.next();
            Object v1 = arg11.b().get(v0);
            if (arg11.d <= 0) {
                arg11.d = arg11.a().getInt("socketTimeout", 5000);
            }

            int v6 = arg11.d;
            if (arg11.f <= 0) {
                arg11.f = arg11.a().getInt("maxAttempts", 3);
            }

            int v7 = arg11.f;
            boolean v2 = false;
            int v4;
            for (v4 = 0; v4 < v7; ++v4) {
                v2 = HostMonitor.a(((b) v0), v6);
                if (v2) {
                    break;
                }
            }

            h v4_1 = new h(v2, arg10);
            if (v4_1.equals(v1)) {
                continue;
            }

            String v6_1 = "HostMonitor";
            StringBuilder v7_1 = new StringBuilder("Host ").append(((b) v0).a).append(" is currently ");
            String v2_1 = v2 ? "reachable" : "unreachable";
            Logger.a(v6_1, v7_1.append(v2_1).append(" on port 80 via ").append(arg10).toString());
            arg11.b().put(v0, v4_1);
            this.a(arg11.c(), ((b) v0), ((h) v1), v4_1);
        }

        arg11.e();
        Logger.a("HostMonitor", "Reachability check finished!");
    }

    private void a(String arg5, b arg6, h arg7, h arg8) {
        HostStatus v0 = new HostStatus().setHost(arg6.a).setPort(80).setPreviousReachable(arg7.a).setPreviousConnectionType(arg7.b).setReachable(arg8.a).setConnectionType(arg8.b);
        Logger.a("HostMonitor", "Broadcast with action: " + arg5 + " and status: " + v0);
        Intent v1 = new Intent(arg5);
        v1.putExtra("HostStatus", v0);
        this.sendBroadcast(v1);
    }

    public void onCreate() {
        try {
            super.onCreate();
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }

    protected void onHandleIntent(Intent arg11) {
        Iterator v5;
        if (arg11 == null) {
            return;
        }

        try {
            if ("com.bytedance.ttnet.hostmonitor.check".equals(arg11.getAction())) {
                e v4 = new e(((Context) this));
                if (v4.b().isEmpty()) {
                    Logger.a("HostMonitor", "No hosts to check at this moment");
                    return;
                }

                int v1_1 = arg11.getIntExtra("com.bytedance.ttnet.hostmonitor.connection_type", -1);
                ConnectionType v3 = v1_1 < 0 ? HostMonitor.c(((Context) this)) : ConnectionType.values()[v1_1];
                if (v3 == ConnectionType.NONE) {
                    Logger.a("HostMonitor", "No active connection. Notifying that all the hosts are unreachable");
                    v5 = v4.b().keySet().iterator();
                } else {
                    this.a(v3, v4);
                    return;
                }

                while (v5.hasNext()) {
                    Object v2 = v5.next();
                    Object v1_2 = v4.b().get(v2);
                    h v6 = new h(false, v3);
                    if (v6.equals(v1_2)) {
                        continue;
                    }

                    Logger.a("HostMonitor", "Host " + ((b) v2).a + " is currently unreachable on port 80");
                    v4.b().put(v2, v6);
                    this.a(v4.c(), ((b) v2), ((h) v1_2), v6);
                }

                v4.e();
            }

            return;
        } catch (Throwable v1) {
            v1.printStackTrace();
            return;
        }
    }
}

